package com.xinou.daily_report.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.util.BeanUtil;
import com.xinou.daily_report.PO.okForm.*;

import com.xinou.daily_report.VO.OkFormCountVO;
import com.xinou.daily_report.VO.OkFormInfoVO;
import com.xinou.daily_report.entity.SysOkForm;
import com.xinou.daily_report.enumerate.SysConfigEnum;
import com.xinou.daily_report.mapper.SysOkFormMapper;
import com.xinou.daily_report.service.SysOkFormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xinou.daily_report.utils.ConstUtil;
import com.xinou.sso.service.SysUserService;
import com.xinou.sso.util.ShiroUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-07-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysOkFormServiceImpl extends ServiceImpl<SysOkFormMapper, SysOkForm> implements SysOkFormService {

    final SysUserService sysUserService;

    @Override
    public ApiResponse<String> okFormSave(OkFormAddPO po) {
        //System.err.println(po);
        if(po.getCompleteDate().isBefore(LocalDateTime.now())){
            throw new BusException("截止日期必须在今日以后");
        }
        SysOkForm sysOkForm = BeanUtil.beanA2beanB(po, SysOkForm.class);
        if (!save(sysOkForm)) {
            throw new BusException("添加失败");
        }
        return new ApiResponse<>("添加成功");
    }

    @Override
    public ApiResponse<OkFormInfoVO> okFormList(OkFormListPO po) {
        Page<SysOkForm> page = new Page<>(po.getPageNumber(), po.getPageSize());
        po.setApproachDay((SysConfigEnum.OK_FORM_APPROACH_DAY.getValue() == null ? 0 : SysConfigEnum.OK_FORM_APPROACH_DAY.getValue()));
        return new ApiResponse<>(this.baseMapper.okFormList(page, po));
    }

    @Override
    public ApiResponse<OkFormCountVO> okFormCount() {
        return new ApiResponse<>(this.baseMapper.okFormCount(
                (SysConfigEnum.OK_FORM_APPROACH_DAY.getValue() == null ? 0 : SysConfigEnum.OK_FORM_APPROACH_DAY.getValue()),
                ShiroUtil.getUserId()
        ));
    }

    @Override
    public ApiResponse<String> okFormDelete(OkFormDeletePO po) {
        if (!removeById(po.getId())) {
            throw new BusException("删除失败");
        }
        return new ApiResponse<>("删除成功！");
    }

    @Override
    public ApiResponse<String> okFormUpdate(OkFormUpdatePO po) {
        if (!updateById(BeanUtil.beanA2beanB(po, SysOkForm.class))) {
            throw new BusException("修改失败");
        }
        return new ApiResponse<>("修改成功！");
    }

    @Override
    public ApiResponse<Void> okFormStatus(OkFormStatusPO po) {
        SysOkForm sysOkForm = getById(po.getId());
        if (sysOkForm == null) {
            throw new BusException("没有此ok单！");
        }
        //Integer status = po.getStatus();
        //if (status.equals(ConstUtil.OK_FORM_FINISHED)) {
        //    if (sysOkForm.getLiablerId().equals(ShiroUtil.getUserId())) {
        //        updateById(sysOkForm.setStatus(po.getStatus()));
        //    } else {
        //        throw new BusException("你不是责任人，无法操作！");
        //    }
        //} else if (status.equals(ConstUtil.OK_FORM_CONFIRMED)) {
        //    if (sysOkForm.getCommanderId().equals(ShiroUtil.getUserId())) {
        //        updateById(sysOkForm.setStatus(po.getStatus()));
        //    } else {
        //        throw new BusException("你不是指令人，无法操作！");
        //    }
        //} else {
        //    throw new BusException("操作失败！");
        //}
        if(po.getStatus().equals(ConstUtil.OK_FORM_FINISHED)){
            sysOkForm.setFinishTime(LocalDateTime.now());
        }
        updateById(sysOkForm.setStatus(po.getStatus()));
        return new ApiResponse<>();
    }

}
